package routes

import (
	"ginadmin/utils"
	"github.com/gin-gonic/gin"
	"net/http"
	"os"
)

func CommonHandler(engine *gin.Engine) {

	engine.StaticFS("/view", http.Dir("./view"))
	engine.StaticFS("/css", http.Dir("./view/css"))
	engine.StaticFS("/fonts", http.Dir("./view/fonts"))
	engine.StaticFS("/images", http.Dir("./view/images"))
	engine.StaticFS("/js", http.Dir("./view/js"))
	engine.StaticFS("/lib", http.Dir("./view/lib"))
	engine.StaticFS("/menu.json", http.Dir("./view/menu.json"))
	engine.StaticFS("/treetable.json", http.Dir("./view/treetable.json"))
	engine.POST("/login", LoginForm)
	engine.GET("/indexpage", loginSuccpage)
	engine.GET("/", IndexPage)
	webGroup := engine.Group("/web")
	webGroup.Use(utils.JWTAuth())
	WebRoutesHandler(webGroup)
}

func loginSuccpage(c *gin.Context) {
	file := "view/indexpage.html"
	info, err := os.Stat(file)
	if err == nil && !info.IsDir() {
		http.ServeFile(c.Writer, c.Request, file)
	}
}

func IndexPage(c *gin.Context) {
	file := "view/login.html"
	info, err := os.Stat(file)
	if err == nil && !info.IsDir() {
		http.ServeFile(c.Writer, c.Request, file)
	}
}
